using System.Runtime.InteropServices;

namespace System.Utility.Helper
{
	public class LongPathFileOperator : IDisposable
	{
		private const uint GENERIC_READ = 2147483648;

		private const uint OPEN_EXISTING = 3;

		private const string PathHead = "\\\\?\\";

		private IntPtr handle;

		public LongPathFileOperator()
		{
		}

		public bool Close()
		{
			return CloseHandle(handle);
		}

		[DllImport("kernel32", CharSet=CharSet.None, ExactSpelling=false, SetLastError=true)]
		private static extern bool CloseHandle(IntPtr hObject);

		public static bool Copy(string source, string target, bool overwrite)
		{
			string str = string.Concat("\\\\?\\", source);
			string str1 = string.Concat("\\\\?\\", target);
			return CopyFile(str, str1, !overwrite);
		}

		[DllImport("kernel32.dll", CharSet=CharSet.Unicode, ExactSpelling=false)]
		private static extern bool CopyFile(string source, string target, bool overwrite);

		[DllImport("kernel32", CharSet=CharSet.None, ExactSpelling=false, SetLastError=true)]
		private static extern IntPtr CreateFile(string FileName, uint DesiredAccess, uint ShareMode, uint SecurityAttributes, uint CreationDisposition, uint FlagsAndAttributes, int hTemplateFile);

		public void Dispose()
		{
			Close();
		}

		[DllImport("XFileParser.dll", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.None, ExactSpelling=false)]
		private static extern IntPtr GetTextFromFile(string pFileName, ref int pCount);

		//public bool Open(string FileName)
		//{
		//	bool flag;
		//	this.handle = LongPathFileOperator.CreateFile(FileName, -2147483648, 0, 0, 3, 0, 0);
		//	flag = (!(this.handle != IntPtr.Zero) ? false : true);
		//	return flag;
		//}

		//public int Read(byte[] buffer, int index, int count)
		//{
		//	unsafe
		//	{
		//		byte* numPointer;
		//		int num;
		//		int num1 = 0;
		//		byte[] numArray = buffer;
		//		byte[] numArray1 = numArray;
		//		if (numArray == null || (int)numArray1.Length == 0)
		//		{
		//			numPointer = null;
		//		}
		//		else
		//		{
		//			numPointer = &numArray1[0];
		//		}
		//		if (LongPathFileOperator.ReadFile(this.handle, numPointer + index, count, ref num1, 0))
		//		{
		//			numPointer = null;
		//			num = num1;
		//		}
		//		else
		//		{
		//			num = 0;
		//		}
		//		return num;
		//	}
		//}

		[DllImport("kernel32", CharSet=CharSet.None, ExactSpelling=false, SetLastError=true)]
		private static extern unsafe bool ReadFile(IntPtr hFile, void* pBuffer, int NumberOfBytesToRead, int* pNumberOfBytesRead, int Overlapped);

		public string TryParse(string filePath, out bool isSucess)
		{
			string empty;
			int num = -1;
			IntPtr textFromFile = GetTextFromFile(filePath, ref num);
			if (num > 0)
			{
				string ansiString = IntptrExtension.ToAnsiString(textFromFile);
				Marshal.FreeHGlobal(textFromFile);
				isSucess = true;
				empty = ansiString;
			}
			else
			{
				isSucess = false;
				empty = string.Empty;
			}
			return empty;
		}
	}
}